package com.foreverwin.jzymes.production.picking.controller;

import com.foreverwin.jzymes.common.service.ICustomFieldsAdviceService;
import com.foreverwin.jzymes.common.service.IItemAdviceService;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.picking.domain.PickingBom;
import com.foreverwin.jzymes.production.picking.service.IPickingAdviceService;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/production/picking")
public class PickingController extends BaseController {
    @Autowired
    IItemAdviceService itemAdviceService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    IPickingAdviceService pickingAdviceService;

    /**
     * 检查物料数量
     *
     * @param jsonMap
     */
    @RequestMapping("/exsitqty")
    public  AjaxResult exsitqty( @RequestParam Map <String,Object> jsonMap ){
        try{
            String site = CommonMethods.getSite();
            Assert.notNull( jsonMap.get("qty"), I18nUtil.getI18nText( "production.cutter.qty" ) );
            Assert.hasText( site , I18nUtil.getI18nText( "common.siteNotBlank" ) );
            Assert.notNull( jsonMap.get("item") , I18nUtil.getI18nText( "common.itemNotBlank" )  );

            Map<String,Object> itemMap = itemAdviceService.existItem( site , (String)jsonMap.get("item") );
            String type = (String)itemMap.get( "QUANTITY_RESTRICTION" );
            BigDecimal b = new BigDecimal((String)jsonMap.get("qty"));
            if ( type.equals("W") ){
                Assert.isTrue( new BigDecimal(b.intValue()).compareTo(b)==0 ,I18nUtil.getI18nText(  "production.picking.pickingBomqty" ,new String[] {(String)jsonMap.get("item")} ) );
            }

            return AjaxResult.returnSuccess().put( "value",jsonMap );
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 保存领料单据信息
     *
     * @param jsonMap
     */
    @RequestMapping("/submit")
    public  AjaxResult submit( @RequestBody Map<String,Object> jsonMap ){
        try{
            String site = CommonMethods.getSite();
            jsonMap.put( "site" , site );
            String createUserBo =  CommonMethods.getUserBo();
            jsonMap.put("createUserBo" ,createUserBo);

            if (StringUtils.isBlank( (String)jsonMap.get( "bill" ) )){
                pickingAdviceService.insertPicking( jsonMap );
            }else {
                pickingAdviceService.updatePicking( jsonMap );
            }

            return AjaxResult.returnSuccess().put("value" ,jsonMap);
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }
    /**
     * 发送领料单据信息
     *
     * @param jsonMap
     */
    @RequestMapping("/send")
    public  AjaxResult send(@RequestBody Map<String,Object> jsonMap){
        try{
            String site = CommonMethods.getSite();
            jsonMap.put( "site" , site );

            pickingAdviceService.sendPicking( jsonMap );
            return AjaxResult.returnSuccess();
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

    /**
     * 检索领料单据信息
     *
     * @param jsonMap
     */
    @RequestMapping("/retrieve")
    public  AjaxResult  retrieve(@RequestParam Map<String,Object> jsonMap ){
        try{
            String site = CommonMethods.getSite();
            jsonMap.put( "site" , site );

            Map< String, Object > map = pickingAdviceService.selectPicking( jsonMap );

            return AjaxResult.returnSuccess().put("value" , map);
        }catch ( BusinessException e ){
            return AjaxResult.returnError( e.getMessage() );
        }
    }

}
